package cn.com.ghy.android.widget.chat;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.ghy.android.bean.CustomerInfoBean;
import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.bean.OrderDetailBean;
import cn.com.ghy.android.exception.DisposeException;
import cn.com.ghy.android.service.OrderOpeService;
import cn.com.ghy.android.style.MenuTabContent;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.ComStaticApp;
import cn.com.ghy.android.widget.R;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:订单信息
 * @author wuz  @date 2011-5-5 10:42:18
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class OrderInfo extends TabActivity {

	private TextView labTxtOrderTotalPrice = null;
	private TextView labTxtCustomerId = null;
	private TextView labTxtCustomerName = null;
	private TextView labTxtFillOrderPerson = null;
	private TabHost tabHost = null;
	private LinearLayout linFax = null;
	private CheckBox chkIsFax = null;
	private CheckBox chkIsCar = null;
	private EditText txtFax = null;

	private EditText txtHandOrderId = null;

	private Button btnOrderGoodsDate = null;

//	private TextView labTxtContact = null;
	private TextView labTxtPhone = null;
	private TextView labTxtSendGoodsAddress = null;

	private ListView listView = null;
	private LinearLayout layChangeBusiness = null;

	private Intent intent = null;
	private Bundle bundle = null;
	private CustomerInfoBean customerInfo = null;
	private LoginInfoBean loginInfo = null;
	private List<OrderDetailBean> listOrderDetail = null;

	private ProgressDialog progressDialog = null;

	private ImageButton btnList = null;
	private ImageButton btnBack = null;

	private HttpThread thread = null;

	private int mYear = 2011;
	private int mMonth = 0;
	private int mDay = 1;

	private MenuTabContent.MenuBodyAdapter bodyAdapter = null;
	private MenuTabContent tabMenu;
	private View appToolBar = null;

	private OrderInfoAdapter orderInfoAdapter = null;
	private String intentflag = null;

	private DisposeException disposeException = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_info);
		getSession();
		tabHost = this.getTabHost();
		TabWidget tabWidget = tabHost.getTabWidget();

		tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("基本信息")
				.setContent(R.id.layOrderBaseInfo));
		tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("订单明细")
				.setContent(R.id.layOrderDetail));
		setMenu(tabHost.getCurrentTabTag());
		for (int i = 0; i < tabWidget.getChildCount(); i++) {
			tabWidget.getChildAt(i).getLayoutParams().height = this.getWindowManager()
					.getDefaultDisplay().getHeight() / 16;
		}
		setToolBar();
		findView();
		init();
		addListener();
	}

	@SuppressWarnings("unchecked")
	public void getSession() {
		disposeException = ((ComStaticApp) getApplication()).getDisposeException();
		loginInfo = ((ComStaticApp) this.getApplication()).getLoginInfo();
		intent = this.getIntent();
		customerInfo = (CustomerInfoBean) intent.getSerializableExtra("customerInfo");
		listOrderDetail = (List<OrderDetailBean>) intent.getSerializableExtra("listOrderDetail");
		intentflag = intent.getStringExtra("intentflag");
	}

	private void setToolBar() {
		appToolBar = findViewById(R.id.appToolBar);
		btnList = (ImageButton) appToolBar.findViewById(R.id.btnList);
		btnBack = (ImageButton) appToolBar.findViewById(R.id.btnBack);
	}

	public void findView() {
		labTxtOrderTotalPrice = (TextView) this.findViewById(R.id.labTxtOrderTotalPrice);
		labTxtCustomerId = (TextView) this.findViewById(R.id.labTxtCustomerId);
		labTxtCustomerName = (TextView) this.findViewById(R.id.labTxtCustomerName);
		labTxtFillOrderPerson = (TextView) this.findViewById(R.id.labTxtFillOrderPerson);

//		labTxtContact = (TextView) this.findViewById(R.id.labTxtContact);
		labTxtPhone = (TextView) this.findViewById(R.id.labTxtPhone);
		labTxtSendGoodsAddress = (TextView) this.findViewById(R.id.labTxtSendGoodsAddress);

		txtHandOrderId = (EditText) this.findViewById(R.id.txtHandOrderId);
		btnOrderGoodsDate = (Button) this.findViewById(R.id.btnOrderGoodsDate);
		linFax = (LinearLayout) this.findViewById(R.id.linFax);
		chkIsFax = (CheckBox) this.findViewById(R.id.chkIsFax);
		chkIsCar = (CheckBox) this.findViewById(R.id.chkIsCar);
		txtFax = (EditText) this.findViewById(R.id.txtFax);
		if (null != intentflag && "modify".equals(intentflag)) {
			linFax.setVisibility(View.GONE);
		}

		listView = (ListView) this.findViewById(R.id.listOrderDetailList);
		int totleHeight = this.getWindowManager().getDefaultDisplay().getHeight();
		LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) listView
				.getLayoutParams();
		linearParams.height = (int) (totleHeight / 1.62);
		listView.setLayoutParams(linearParams);

		layChangeBusiness = (LinearLayout) this.findViewById(R.id.layChangeBusiness);
	}

	public void init() {
		labTxtCustomerId.setText(customerInfo.getPartner());
		labTxtCustomerName.setText(customerInfo.getName_org1());
//		labTxtContact.setText(customerInfo.getLxr());
		labTxtPhone.setText(customerInfo.getLxdh());
		labTxtSendGoodsAddress.setText(customerInfo.getStreet());
		labTxtFillOrderPerson.setText(loginInfo.getPernr() + " " + loginInfo.getDspname());
		
		if(null!=customerInfo.getIsCar()&&"Y".equals(customerInfo.getIsCar())){
			chkIsCar.setChecked(true);
		}else{
			chkIsCar.setChecked(false);
			
		}
		
		
		LinearLayout.LayoutParams layoutParamsLab = new LinearLayout.LayoutParams(0,
				LayoutParams.WRAP_CONTENT);
		layoutParamsLab.setMargins(0, 0, 3, 0);
		layoutParamsLab.weight = 7;
		TextView tvLab = new TextView(OrderInfo.this);
		tvLab.setGravity(Gravity.RIGHT);
		tvLab.setTextSize(15);

		LinearLayout.LayoutParams layoutParamsTxt = new LinearLayout.LayoutParams(0,
				LayoutParams.WRAP_CONTENT);
		layoutParamsTxt.setMargins(0, 0, 10, 0);
		layoutParamsTxt.weight = 25;
		TextView tvTxt = new TextView(OrderInfo.this);
		tvTxt.setTextSize(15);

		if (null != customerInfo.getJxsid() && null != customerInfo.getJxsname()) {
			tvLab.setText("经销商:");
			tvTxt.setText(customerInfo.getJxsid() + " " + customerInfo.getJxsname());
		} else if (null != customerInfo.getFxsid() && null != customerInfo.getFxsname()) {
			tvLab.setText("分销商:");
			tvTxt.setText(customerInfo.getFxsid() + " " + customerInfo.getFxsname());
		}
		layChangeBusiness.addView(tvLab, layoutParamsLab);
		layChangeBusiness.addView(tvTxt, layoutParamsTxt);

		final Calendar c = Calendar.getInstance();
		mYear = c.get(Calendar.YEAR);
		mMonth = c.get(Calendar.MONTH);
		mDay = c.get(Calendar.DAY_OF_MONTH);
		if (null != customerInfo.getOrderReportInfoBean()) {
			btnOrderGoodsDate.setText(customerInfo.getOrderReportInfoBean().getXqriqi()
					.substring(0, 10));
			txtHandOrderId.setText(customerInfo.getOrderReportInfoBean().getOrderno());
		}

		labTxtOrderTotalPrice.setText("0.00");

		orderInfoAdapter = new OrderInfoAdapter(this);
		listView.setAdapter(orderInfoAdapter);
		listView.setOnItemClickListener(new OrderDataLisEvent());

		if (null != listOrderDetail && listOrderDetail.size() > 0) {
			for (int i = 0; i < listOrderDetail.size(); i++) {
				orderInfoAdapter.addOrder(listOrderDetail.get(i));
			}
		}
	}

	public void addListener() {
		btnOrderGoodsDate.setOnClickListener(btnListener);
		btnList.setOnClickListener(btnListener);
		chkIsFax.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					txtFax.setEnabled(true);
					customerInfo.setTosend("Y");
				} else {
					txtFax.setEnabled(false);
					customerInfo.setTosend("N");
				}
			}
		});
		
		chkIsCar.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					customerInfo.setIsCar("Y");
				} else {
					customerInfo.setIsCar("N");
				}
			}
		});

		tabHost.setOnTabChangedListener(new OnTabChangeListener() {

			@Override
			public void onTabChanged(String tabId) {
				setMenu(tabId);
			}

		});

		btnBack.setOnClickListener(btnListener);
	}

	private class OrderInfoAdapter extends BaseAdapter {

		private Context context = null;
		private List<OrderDetailBean> list = new ArrayList<OrderDetailBean>();

		public OrderInfoAdapter(Context context) {
			this.context = context;
		}

		public int getCount() {
			return list.size();
		}

		public OrderDetailBean getItem(int position) {
			return list.get(position);
		}

		public List<OrderDetailBean> getItemAll() {
			return list;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(inflater);
			LinearLayout linearLayout = null;
			if (list.get(position) instanceof OrderDetailBean) {
				linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.order_info_lay, null);
				TextView textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderInfoSKU));
				textView.setText(list.get(position).getBismt());

				textView = ((TextView) linearLayout.findViewById(R.id.labTxtMatnr));
				textView.setText(list.get(position).getMatnr());

				textView = ((TextView) linearLayout.findViewById(R.id.labTxtMaktx));
				textView.setText(list.get(position).getMaktx());

				textView = ((TextView) linearLayout.findViewById(R.id.labTxtTotalprice));
				textView.setText(list.get(position).getTotalprice() + "元");

				textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderInfoNum));
				textView.setText(list.get(position).getNum()
						+ list.get(position).getUnitInfo().getUnitname());

				textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderPrice));
				textView.setText(list.get(position).getOrderprice() + "元");

				textView = ((TextView) linearLayout.findViewById(R.id.labTxtOrderComments));
				textView.setText(list.get(position).getRemark());
			}
			return linearLayout;
		}

		public void addOrder(OrderDetailBean orderDetail) {
			for (int i = 0; i < list.size(); i++) {
				if (orderDetail.getBismt().equals(list.get(i).getBismt())) {
					Toast.makeText(OrderInfo.this, "SKU:" + orderDetail.getBismt() + "不能重复!",
							Toast.LENGTH_SHORT).show();
					return;
				}
			}
			list.add(orderDetail);
			totalPriceCount(list);
			notifyDataSetChanged();
		}

		public void remove(int index, OrderDetailBean orderDetail) {
			if (index < 0)
				return;
			list.remove(index);
			totalPriceCount(list);
			notifyDataSetChanged();
		}

		public void modify(int index, OrderDetailBean orderDetail) {
			if (index < 0)
				return;
			list.remove(index);
			list.add(index, orderDetail);
			totalPriceCount(list);
			notifyDataSetChanged();
		}

		private void totalPriceCount(List<OrderDetailBean> list) {
			double totalPrice = 0;
			for (OrderDetailBean orderDetail : list) {
				totalPrice += Double.parseDouble(orderDetail.getTotalprice());
			}
			java.text.DecimalFormat df = new java.text.DecimalFormat("######0.00");
			labTxtOrderTotalPrice.setText(df.format(totalPrice));
		}

		public void removeAll() {
			if (null != list && list.size() > 0) {
				list.removeAll(list);
				notifyDataSetChanged();
				labTxtOrderTotalPrice.setText("0.00");
			}
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
			case CommonStatic.INTENT_REQUEST_CODE :
				if (resultCode == RESULT_OK) {
					bundle = data.getExtras();
					OrderDetailBean orderDetail = (OrderDetailBean) bundle
							.getSerializable("orderDetail");
					int position = -1;
					position = bundle.getInt("position");
					if (position != -1) {
						orderInfoAdapter.modify(position, orderDetail);
					} else {
						orderInfoAdapter.addOrder(orderDetail);
					}
				}
				break;
			default :
				break;
		}
	}

	private OnDateSetListener orderGoodsDateLis = new DatePickerDialog.OnDateSetListener() {

		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
			mYear = year;
			mMonth = monthOfYear;
			mDay = dayOfMonth;
			btnOrderGoodsDate.setText(mYear + "-" + (mMonth + 1) + "-" + mDay);
		}
	};

	private class OrderDataLisEvent implements AdapterView.OnItemClickListener {

		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			OrderInfoAdapter orderInfoAdapter = (OrderInfoAdapter) (parent.getAdapter());
			showDialog(OrderInfo.this, position, orderInfoAdapter.getItem(position));

		}
	}

	private View.OnClickListener btnListener = new View.OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.btnOrderGoodsDate :
					showDialog(R.id.btnOrderGoodsDate);
					break;
				case R.id.btnList :
					openOptionsMenu();
					break;
				case R.id.btnBack :
					new AlertDialog.Builder(OrderInfo.this).setTitle("提示信息")
							.setMessage("确定要退出操作的订单?")
							.setPositiveButton("确定", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dlg, int sumthin) {
									OrderInfo.this.finish();
								}
							}).setNeutralButton("取消", null).show();
					break;
			}
		}
	};

	private void createOrder() {
		final String orderno = txtHandOrderId.getText().toString();
		final String xqriqi = btnOrderGoodsDate.getText().toString();
		List<OrderDetailBean> listOrderDetailAll = orderInfoAdapter.getItemAll();
		if ("".equals(labTxtCustomerName.getText())) {
			Toast.makeText(OrderInfo.this, "提示信息:客户名称不能为空,请联系销售支持进行维护!", Toast.LENGTH_SHORT).show();
			return;
		}
//		if ("".equals(labTxtContact.getText())) {
//			Toast.makeText(OrderInfo.this, "提示信息:联系人不能为空,请联系销售支持进行维护!", Toast.LENGTH_LONG).show();
//			return;
//		}
		if ("".equals(labTxtPhone.getText())) {
			Toast.makeText(OrderInfo.this, "提示信息:联系电话不能为空,请联系销售支持进行维护!", Toast.LENGTH_LONG).show();
			return;
		}
		if ("".equals(labTxtSendGoodsAddress.getText())) {
			Toast.makeText(OrderInfo.this, "提示信息:送货地址不能为空,请联系销售支持进行维护!", Toast.LENGTH_LONG).show();
			return;
		}
		if (customerInfo.getJxsid() == null && customerInfo.getFxsid() == null) {
			Toast.makeText(OrderInfo.this, "提示信息:请联系销售支持维护该客户对应的经销商或分销商关系！", Toast.LENGTH_LONG)
					.show();
			return;
		}
		if (null != xqriqi && !"更改".equals(xqriqi)) {
			if (null != listOrderDetailAll && listOrderDetailAll.size() > 0) {
				final List<OrderDetailBean> listOd = new ArrayList<OrderDetailBean>();
				for (Iterator<OrderDetailBean> it = listOrderDetailAll.iterator(); it.hasNext();) {
					OrderDetailBean od = it.next();
					if (!"0".equals(od.getNum())) {
						listOd.add(od);
					}
				}
				if (null != listOd && listOd.size() > 0) {
					String msg = "确认提交订单?";
					if ("modify".equals(intentflag)) {
						msg = "确认提交修改后的订单?";
					} else {
						if (chkIsFax.isChecked()) {
							String fax = txtFax.getText().toString().trim();
							if ("".equals(fax)) {
								Toast.makeText(OrderInfo.this, "请输入传真号码!", Toast.LENGTH_SHORT)
										.show();
								return;
							} else {
								customerInfo.setFax(fax);
							}
						}
					}
					new AlertDialog.Builder(OrderInfo.this).setTitle("提示信息").setMessage(msg)
							.setPositiveButton("确定", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dlg, int sumthin) {
									thread = new HttpThread(handler);
									thread.doStart(customerInfo, orderno, xqriqi, listOd, loginInfo);
								}
							}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dlg, int sumthin) {
								}
							}).show();
				} else {
					Toast.makeText(OrderInfo.this, "请添加有效的订单信息!", Toast.LENGTH_SHORT).show();
					return;
				}
			} else {
				Toast.makeText(OrderInfo.this, "请添加订单信息!", Toast.LENGTH_SHORT).show();
				return;
			}
		} else {
			Toast.makeText(OrderInfo.this, "请选择要货日期!", Toast.LENGTH_SHORT).show();
			return;
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
			case R.id.btnOrderGoodsDate :
				return new DatePickerDialog(this, orderGoodsDateLis, mYear, mMonth, mDay);
		}
		return null;
	}

	private void showDialog(final Context context, final int position,
			final OrderDetailBean orderDetail) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("提示信息:");
		builder.setMessage("SKU编号 " + orderDetail.getBismt());
		builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				intent = new Intent(OrderInfo.this, OrderInfoModifyPop.class);
				bundle = new Bundle();
				bundle.putInt("position", position);
				bundle.putSerializable("OrderDetail", orderDetail);
				intent.putExtras(bundle);
				OrderInfo.this.startActivityForResult(intent, CommonStatic.INTENT_REQUEST_CODE);
			}
		});
		builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				orderInfoAdapter.remove(position, orderDetail);
			}
		});
		builder.show();
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message m) {
			switch (m.what) {
				case CommonStatic.STATE_NORMAL :
					String result = (String) m.getData().getSerializable("result");
					if ("OK".equals(result)) {
						String msg = "";
						if ("modify".equals(intentflag)) {
							msg = "修改订单成功!";
						} else {
							msg = "提交订单成功!";
						}
						new AlertDialog.Builder(OrderInfo.this).setTitle("提示信息").setMessage(msg)
								.setNeutralButton("确定", new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dlg, int sumthin) {
										OrderInfo.this.finish();
									}
								}).show();
					} else if ("OK-faxfalse".equals(result.substring(0, 11))) {
						new AlertDialog.Builder(OrderInfo.this)
								.setTitle("提示信息")
								.setMessage(
										"订单提交成功！(传真发送失败," + result.substring(11) + ",请在查询订单处重发传真.)")
								.setNeutralButton("关闭", new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dlg, int sumthin) {
										OrderInfo.this.finish();
									}
								}).show();
					} else {
						new AlertDialog.Builder(OrderInfo.this).setTitle("提示信息")
								.setMessage("提交订单失败," + result).setNeutralButton("关闭", null).show();
					}
					break;
			}
		}
	};

	private class HttpThread extends Thread {
		private Handler handle = null;
		private CustomerInfoBean customerInfo = null;
		private String orderno = null;
		private String xqriqi = null;
		private List<OrderDetailBean> list = null;

		public HttpThread(Handler hander) {
			handle = hander;
		}

		private void doStart(CustomerInfoBean customerInfo, String orderno, String xqriqi,
				List<OrderDetailBean> list, LoginInfoBean loginInfo) {
			this.customerInfo = customerInfo;
			this.orderno = orderno;
			this.xqriqi = xqriqi;
			this.list = list;
			progressDialog = new ProgressDialog(OrderInfo.this);
			progressDialog.setTitle("提示信息");
			progressDialog.setMessage("正在请求数据，请稍等......");
			progressDialog.setCancelable(false);
			progressDialog.show();
			this.start();
		}

		public void run() {
			Bundle bundle = null;
			try {
				String result = null;
				OrderOpeService orderOpeService = new OrderOpeService();
				if (null != customerInfo.getOrderReportInfoBean()) {
					result = orderOpeService.OrderEdit(customerInfo, orderno, xqriqi, list,
							loginInfo);
				} else {
					result = orderOpeService.OrderAdd(customerInfo, orderno, xqriqi, list,
							loginInfo);
				}
				progressDialog.dismiss();
				Message message = handle.obtainMessage();
				bundle = new Bundle();
				message.what = CommonStatic.STATE_NORMAL;
				bundle.putSerializable("result", result);
				message.setData(bundle);
				handle.sendMessage(message);
			} catch (Exception ex) {
				progressDialog.dismiss();
				disposeException.handleException(ex);
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			new AlertDialog.Builder(OrderInfo.this).setTitle("提示信息").setMessage("确定要退出操作的订单?")
					.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dlg, int sumthin) {
							OrderInfo.this.finish();
						}
					}).setNeutralButton("取消", null).show();
		}
		return true;
	}

	private void setMenu(String tabId) {
		String flag = "提交订单";
		if (null != intentflag && "modify".equals(intentflag)) {
			flag = "修改订单";
		}
		if ("tab1".equals(tabId)) {
			bodyAdapter = new MenuTabContent.MenuBodyAdapter(this, new String[] { flag },
					new int[] { R.drawable.data_coll_menu_submit }, 13, getResources().getColor(
							R.color.white));
		} else if ("tab2".equals(tabId)) {
			bodyAdapter = new MenuTabContent.MenuBodyAdapter(this, new String[] { "添加", "删除全部",
					"预览", flag }, new int[] { R.drawable.menu_add, R.drawable.menu_delete,
					R.drawable.menu_preview, R.drawable.data_coll_menu_submit }, 13, getResources()
					.getColor(R.color.white));
		}

		tabMenu = new MenuTabContent(this, new BodyClickEvent(tabId), getResources().getColor(
				R.color.MenuBackGroundColor), R.style.PopupAnimation);

		tabMenu.update();
		tabMenu.SetBodyAdapter(bodyAdapter);
	}

	private class BodyClickEvent implements OnItemClickListener {
		private String tabId = "";

		public BodyClickEvent(String tabId) {
			super();
			this.tabId = tabId;
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			tabMenu.SetBodySelect(position, Color.GRAY);
			if ("tab1".equals(tabId)) {
				switch (position) {
					case 0 :
						createOrder();
						break;
				}
			} else if ("tab2".equals(tabId)) {
				switch (position) {
					case 0 :
						intent = new Intent(OrderInfo.this, OrderInfoModifyPop.class);
						OrderInfo.this.startActivityForResult(intent,
								CommonStatic.INTENT_REQUEST_CODE);
						break;
					case 1 :
						if (orderInfoAdapter.getItemAll() != null
								&& orderInfoAdapter.getItemAll().size() > 0) {
							new AlertDialog.Builder(OrderInfo.this)
									.setTitle("提示信息")
									.setMessage("确定删除全部订单信息?")
									.setPositiveButton("确定", new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dialog, int whichButton) {
											orderInfoAdapter.removeAll();
										}
									})
									.setNeutralButton("关闭", new DialogInterface.OnClickListener() {
										public void onClick(DialogInterface dlg, int sumthin) {
										}
									}).show();
						} else {
							Toast.makeText(OrderInfo.this, "没有要删除的订单信息!", Toast.LENGTH_SHORT)
									.show();
						}
						break;
					case 2 :
						List<OrderDetailBean> orderDetailAll = orderInfoAdapter.getItemAll();
						if (null != orderDetailAll && orderDetailAll.size() > 0) {
							intent = new Intent(OrderInfo.this, OrderInfoPreview.class);
							bundle = new Bundle();
							bundle.putSerializable("orderDetailAll", (Serializable) orderDetailAll);
							intent.putExtras(bundle);
							OrderInfo.this.startActivity(intent);
						} else {
							Toast.makeText(OrderInfo.this, "没有要预览的订单信息!", Toast.LENGTH_SHORT)
									.show();
						}
						break;
					case 3 :
						createOrder();
						break;
				}
			}
			if (null != tabMenu) {
				if (tabMenu.isShowing()) {
					tabMenu.dismiss();
				}
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add("menu");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		if (tabMenu != null) {
			if (tabMenu.isShowing()) {
				tabMenu.dismiss();
			} else {
				int toolBarHeight = appToolBar.getHeight();
				tabMenu.showAtLocation(findViewById(R.id.appToolBar), Gravity.BOTTOM, 0,
						toolBarHeight);
			}
		}
		return false;
	}
}
